define(['jquery', 'bootstrap', 'userend', 'table', 'form'], function ($, undefined, Userend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init();
            //绑定tabls选中事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                //根据选中项href值获取页名
                var panel = $($(this).attr("href"));
                //如果页名存在
                if (panel.size() > 0) {
                    //回调选中页中的表
                    Controller.table[panel.attr("id")].call(this);
                    //刷新表数据
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');

            });
            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");

        },
        table: {
            first: function () {
                var table1 = $("#table1");
                // 初始化表格
                table1.bootstrapTable({
                    url: 'mall/shop/settlement/index',
                    extend: {
                        index_url: '',
                        add_url: '',
                        edit_url: '',
                        del_url: '',
                        multi_url: '',
                        table: '',
                    },
                    //显示相应工具栏
                    toolbar: '#toolbar1',
                    pk: 'settlementId',
                    sortName: 'settlementId',
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'settlementId', title: __('Settlementid'), visible:false},
                            {field: 'settlementNo', title: __('Settlementno')},
                            {field: 'settlementType', title: __('Settlementtype'), searchList: {"1":__('Settlementtype 1'),"0":__('Settlementtype 0')}, formatter: Table.api.formatter.normal},
                            {field: 'accName', title: __('Accname'), visible:false},
                            {field: 'accNo', title: __('Accno'), visible:false},
                            {field: 'accUser', title: __('Accuser'), visible:false},
                            {field: 'areaName', title: __('Areaname'), visible:false},
                            {field: 'settlementMoney', title: __('Settlementmoney'), operate:'BETWEEN'},
                            {field: 'commissionFee', title: __('Commissionfee'), operate:'BETWEEN'},
                            {field: 'backMoney', title: __('Backmoney'), operate:'BETWEEN'},
                            {field: 'settlementStatus', title: __('Settlementstatus'), searchList: {"1":__('Settlementstatus 1'),"0":__('Settlementstatus 0')}, formatter: Table.api.formatter.status},
                            {field: 'settlementTime', title: __('Settlementtime'), operate:'RANGE', addclass:'datetimerange'},
                            {field: 'remarks', title: __('Remarks')},
                            {field: 'createtime', title: __('Create time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime, visible:false},
                            {field: 'user_id', title: __('User_id'), visible:false},
                            {field: 'operate', title: __('Operate'), table: table1,events: Table.api.events.operate,
                                buttons: [{
                                    name: 'detail',
                                    text: __('Detail'),
                                    classname: 'btn btn-xs btn-info btn-fields btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'mall/shop/settlement/detail'
                                }],
                                formatter: Table.api.formatter.buttons
                            }
                        ]
                    ]
                });
                // 为表格绑定事件
                Table.api.bindevent(table1);
            },
            second: function () {
                var table2 = $("#table2");
                // 初始化表格
                table2.bootstrapTable({
                    url: 'mall/order/order/unsettlement',
                    extend: {
                        index_url: '',
                        add_url: '',
                        edit_url: '',
                        del_url: '',
                        multi_url: '',
                        table: '',
                    },
                    toolbar: '#toolbar2',
                    pk: 'id',
                    sortName: 'id',
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('ID'), visible:false},
                            {field: 'orderunique', title: __('Orderunique'), visible:false},
                            {field: 'orderNo', title: __('Orderno')},
                            {field: 'payType', title: __('Paytype'),searchList: {"0":__('Paytype 0'),"1":__('Paytype 1')}, formatter: Table.api.formatter.normal},
                            {field: 'goodsMoney', title: __('Goodsmoney'), operate:'BETWEEN'},
                            {field: 'deliverMoney', title: __('Delivermoney'), operate:'BETWEEN'},
                            {field: 'totalMoney', title: __('Totalmoney'), operate:'BETWEEN'},
                            {field: 'realTotalMoney', title: __('Realtotalmoney'), operate:'BETWEEN'},
                            {field: 'commissionFee', title: __('Commissionfee'), operate:'BETWEEN'},
                            {field: 'settlement_id', title: __('Settlement_id'), visible:false},
                            {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime, visible:false},
                            {field: 'operate', title: __('Operate'), table: table2,events: Table.api.events.operate,
                                buttons: [{
                                    name: 'detail',
                                    text: __('Detail'),
                                    classname: 'btn btn-xs btn-info btn-fields btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'mall/order/order/detail'
                                },{
                                    name: 'print',
                                    text: __('Print'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-th-list',
                                    url: 'mall/order/order/printf'
                                },{
                                    name: 'settlement',
                                    text: __('Settlement'),
                                    classname: 'btn btn-xs btn-info btn-ajax',
                                    icon: 'fa fa-th-list',
                                    visible: function( row){
                                        return row.status="unappraise" && !row.settlement_id;
                                    },
                                    url: 'mall/order/order/settlement',
                                    refresh: $($('a[data-toggle="tab"]').attr("href")).find(".btn-refresh").trigger("click")
                                }
                                ],
                                formatter: Table.api.formatter.buttons
                            }
                        ]
                    ]
                });
                // 为表格绑定事件
                Table.api.bindevent(table2);
            },
            third: function () {
                var table3 = $("#table3");
                // 初始化表格
                table3.bootstrapTable({
                    url: 'mall/order/order/settlemented',
                    extend: {
                        index_url: '',
                        add_url: '',
                        edit_url: '',
                        del_url: '',
                        multi_url: '',
                        table: '',
                    },
                    toolbar: '#toolbar3',
                    pk: 'id',
                    sortName: 'id',
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('ID'), visible:false},
                            {field: 'orderunique', title: __('Orderunique'), visible:false},
                            {field: 'orderNo', title: __('Orderno')},
                            {field: 'payType', title: __('Paytype'),searchList: {"0":__('Paytype 0'),"1":__('Paytype 1')}, formatter: Table.api.formatter.normal},
                            {field: 'goodsMoney', title: __('Goodsmoney'), operate:'BETWEEN'},
                            {field: 'deliverMoney', title: __('Delivermoney'), operate:'BETWEEN'},
                            {field: 'totalMoney', title: __('Totalmoney'), operate:'BETWEEN'},
                            {field: 'realTotalMoney', title: __('Realtotalmoney'), operate:'BETWEEN'},
                            {field: 'commissionFee', title: __('Commissionfee'), operate:'BETWEEN'},
                            {field: '.settlementNo', title: __('Settlementno'), visible:false},
                            {field: 'settlementTime', title: __('Settlementtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                            {field: 'operate', title: __('Operate'), table: table3,events: Table.api.events.operate,
                                buttons: [{
                                    name: 'detail',
                                    text: __('Detail'),
                                    classname: 'btn btn-xs btn-info btn-fields btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'mall/order/order/detail'
                                },{
                                    name: 'print',
                                    text: __('Print'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-th-list',
                                    url: 'mall/order/order/printf'
                                }],
                                formatter: Table.api.formatter.buttons
                            }
                        ]
                    ]
                });
                // 为表格绑定事件
                Table.api.bindevent(table3);
            }
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
            var settlement_id = $("#c-settlementId").val();
            Controller.api.bindTable(settlement_id);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            bindTable:function(settlement_id){
                Table.api.init({
                    extend: {
                        detail_url: 'mall/shop/settlement/detail/ids/'+settlement_id,
                        table: 'mall_shop_settlement',
                    }
                });
                var table = $("#table");
                // 初始化表格
                table.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.detail_url,
                    pk: 'id',
                    sortName: 'id',
                    columns: [
                        [
                            {field: 'orderNo', title: __('Orderno')},
                            {field: 'payType', title: __('Paytype'),searchList: {"0":__('Paytype 0'),"1":__('Paytype 1')}, formatter: Table.api.formatter.normal},
                            {field: 'goodsMoney', title: __('Goodsmoney'), operate:'BETWEEN'},
                            {field: 'deliverMoney', title: __('Delivermoney'), operate:'BETWEEN'},
                            {field: 'totalMoney', title: __('Totalmoney'), operate:'BETWEEN'},
                            {field: 'realTotalMoney', title: __('Realtotalmoney'), operate:'BETWEEN'},
                            {field: 'commissionFee', title: __('Commissionfee'), operate:'BETWEEN'},
                            {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime, visible:false},
                        ]
                    ],
                    search: false,
                    commonSearch:false,
                    showExport: false,
                    showColumns: false,
                    showToggle: false,
                    pagination: false
                });
                // 为表格绑定事件
                Table.api.bindevent(table);

            },
        }
    };
    return Controller;
});